#pragma once
#include "pugixml.hpp"

typedef enum signal_type
{
	SGN_INVALID = 0,
	SGN_TICK = 1,
	SGN_CYCLE = 2,
	SGN_ARBITRAGE = 101
	
}signal_type;

struct signal_info
{
	lt::sigid_t id;
	signal_type type;
	std::string param;

	signal_info() :id(0), type(SGN_INVALID)
	{}
};

typedef enum strategy_type
{
	STG_DEMO = -1,
	STG_INVALID = 0,
	STG_EMG_1 = 1,
	STG_EMG_2 = 2,
	STG_EMG_3 = 3,
	STG_SSG_1 = 101
}strategy_type;

struct strategy_info
{
	lt::straid_t id;
	strategy_type type;
	bool openable;
	bool closeable;
	std::string param;

	strategy_info() :id(0), type(STG_INVALID), openable(true), closeable(true)
	{}
};

std::tuple<std::vector <signal_info>, std::vector <strategy_info>> get_execute_config(const char* cfg_file,uint32_t date)
{
	std::vector <strategy_info> strategy;
	std::vector <signal_info> signal;
	pugi::xml_document doc;
	doc.load_file(cfg_file);
	pugi::xml_node root = doc.first_child();
	for (pugi::xml_node c = root.first_child(); c; c = c.next_sibling())
	{
		if(c.name()==std::string("execute"))
		{
			uint32_t begin = c.attribute("begin").as_uint();
			uint32_t end = c.attribute("end").as_uint();
			if (begin <= date && date <= end)
			{
				pugi::xml_node sig_node = c.child("signals");
				for (pugi::xml_node sn = sig_node.first_child(); sn; sn = sn.next_sibling())
				{
					signal_info info;
					info.id = sn.attribute("id").as_int();
					info.type = static_cast<signal_type>(sn.attribute("type").as_int());
					info.param = sn.attribute("param").as_string();
					signal.emplace_back(info);
				}

				pugi::xml_node stra_node = c.child("strategys");
				for (pugi::xml_node sn = stra_node.first_child(); sn; sn = sn.next_sibling())
				{
					strategy_info info;
					info.id = sn.attribute("id").as_int();
					info.type = static_cast<strategy_type>(sn.attribute("type").as_int());
					info.openable = sn.attribute("openable").as_bool();
					info.closeable = sn.attribute("closeable").as_bool();
					info.param = sn.attribute("param").as_string();
					strategy.emplace_back(info);
				}
			}
		}
		
	}
	return std::make_tuple(signal, strategy);
}

std::vector<uint32_t> get_trading_day_config(const char* cfg_file)
{
	std::vector<uint32_t> trading_days;
	pugi::xml_document trading_day_doc;
	trading_day_doc.load_file(cfg_file);
	auto trading_day_root = trading_day_doc.first_child();
	for (pugi::xml_node c = trading_day_root.first_child(); c; c = c.next_sibling())
	{
		uint32_t trading_day = std::atoi(c.first_child().value());
		trading_days.emplace_back(trading_day);
	}
	return trading_days;
}

